<?php

include(dirname(__FILE__) . '/../bootstrap/unit.php');
require_once sfConfig::get('sf_symfony_lib_dir') . '/util/sfToolkit.class.php';
require_once sfConfig::get('sf_symfony_lib_dir') . '/yaml/sfYaml.php';

Logger::configure(dirname(__FILE__) . '/log4phpconfig.properties');

$testCases = sfYaml::load(dirname(__FILE__) . '/fixtures/ShiftCalculatorTest_fixtures.yml');

$t = new lime_test(count($testCases['calculateElapsedHours']));
$t->diag('ShiftCalculator.calculateElapsedHours ' . count($testCases['calculateElapsedHours']));
$sc = new ShiftCalculator();

foreach ($testCases['calculateElapsedHours'] as $case) {
    $t->is($sc->calculateElapsedHours($case['input']),
            $case['output'], $case['comment']);
}

//$t = new lime_test(count($testCases['calculateElapsedHoursExceptions']));
//$t->diag('ShiftCalculator.calculateElapsedHours Exceptions' . count($testCases['calculateElapsedHoursExceptions']));
//foreach ($testCases['calculateElapsedHoursExceptions'] as $case) {
//    try {
//        $sc->calculateElapsedHours($case['input']);
//    } catch (Exception $ex) {
//        $t->is($ex->getMessage(),  $case['output'], $case['comment']);
//    }
//}


$t = new lime_test(count($testCases['calculateWorkingHours']));
$t->diag('ShiftCalculator.calculateWorkingHours' . count($testCases['calculateWorkingHours']));

foreach ($testCases['calculateWorkingHours'] as $case) {
    $t->is($sc->calculateWorkingHours($case['input']),
            $case['output'], $case['comment']);
}

$t = new lime_test(count($testCases['stringToHours']));
$t->diag('ShiftCalculator.stringToHours' . count($testCases['stringToHours']));

foreach ($testCases['stringToHours'] as $case) {
    $t->is($sc->stringToHours($case['input']),
            $case['output'], $case['comment']);
}


$t = new lime_test(count($testCases['convertToDateTime'])*2);
$t->diag('ShiftCalculator.convertToDateTime' . count($testCases['convertToDateTime']));

foreach ($testCases['convertToDateTime'] as $case) {
    $dt = $sc->convertToDateTime($case['input']);

    $t->is($dt["start"]->format('Y-m-d H:i:s'),
            $case['output']['start'], $case['comment']. "  Start");
    $t->is($dt["end"]->format('Y-m-d H:i:s'),
            $case['output']['end'], $case['comment']. "  End");
}
//isContained

$t = new lime_test(count($testCases['isContained']));
$t->diag('ShiftCalculator.isContained' . count($testCases['isContained']));

foreach ($testCases['isContained'] as $case) {
    $container = $case['input']['container'];
    $contained = $case['input']['contained'];
    $result = $sc->isContained($container, $contained);
    $t->is($result,
            $case['output'], $case['comment']);
    
}
//distributeHours
$t = new lime_test(count($testCases['distributeHours']));
$t->diag('ShiftCalculator.distributeHours' . count($testCases['distributeHours']));

foreach ($testCases['distributeHours'] as $case) {
    $container = $case['input']['container'];
    $containedInterval = $case['input']['contained']['interval'];
    $relation = ShiftCalculator::SAME_DAY;
    if(count($case['input']['contained'])==2){
        $relation = $case['input']['contained']['relation'];
    }
    $result = $sc->distributeHours($container, $containedInterval,$relation);
    $t->is($result,
            $case['output'], $case['comment']);

}

//$startDate = new DateTime('2010-01-01 22:00:00');
//$endDate = new DateTime('2010-01-02 02:00:00');
//
//$interval = $startDate->diff($endDate);
//echo $interval->format('%R%H:%I horas') ."\n";
//var_dump($endDate > $startDate);
//
//$startDate = new DateTime('2010-01-01 22:30');
//$endDate = new DateTime('2010-01-02 02:15:00');
//
//$interval = $startDate->diff($endDate);
//echo $interval->format('%R%H:%I horas')."\n";
//var_dump($endDate > $startDate);
//
//$startDate = new DateTime('22:33');
//$endDate = new DateTime('02:15:00');
//
//$interval = $startDate->diff($endDate);
//echo $interval->format('%R%H:%I horas')."\n";
//var_dump($endDate > $startDate);
//
//$startDate = new DateTime('22:33');
//$endDate = new DateTime('23:15:00');
//
//$interval = $startDate->diff($endDate);
//echo $interval->format('%R%H:%I horas')."\n";
// var_dump($endDate > $startDate);

?>
